interface ListWorkspacesParams {
  userKey: string;
}

export interface WorkspaceItem {
  label: string;
  workspaceId: string;
  workspaceName: string;
  dbType: string;
  dbHost: string;
  dbPort: string;
}

export interface AnalysisAndSummaryResponse {
  analysis: CreateAnalysisResponse;
  analysisSummary: GetAnalysisSummaryResponse;
}

export interface Workspace {
  workspaceId: string;
  workspaceName: string;
  workspaceDefinitionId: string;
  dbType: string;
  dbHost: string;
  dbPort: string;
  createTime: string;
  numberOfAnalysis: number;
  latestAnalysisTime: string;
  status: string;
}

export interface AnalysisBasicRead {
  analysisId: string;
  analysisName: string;
  workspaceId: string;
  workspaceName: string;
  status: string;
  numberOfQuery: number;
  numberOfSyntaxError: number;
  numberOfIndex: number;
  numberOfRewrite: number;
  numberOfRewriteRules: number;
  numberOfViolations: number;
  numberOfViolatedQuery: number;
  performanceImprove: number | null;
  createUserId: string;
  createUserName: string;
  createTime: string;
}

export interface ListWorkspacesResponse {
  code: number;
  message: string;
  data: {
    records: Workspace[];
    total: string;
    size: string;
    current: string;
    pages: string;
  };
}

export interface ListAnalysesResponse {
  code: number;
  message: string;
  data: {
    records: AnalysisBasicRead[];
    total: string;
    size: string;
    current: string;
    pages: string;
  };
}

export interface CreateAnalysisParams {
  userKey: string;
  workspace: string;
  dbType?: string;
  workload: string;
  queryMode: string;
  validateFlag?: boolean;
  analysisName?: string;
  analyzeFlag?: boolean;
  deduplicateFlag?: boolean;
  indexOnly?: boolean;
  maxMembersForIndexOnly?: number;
  maxMembers?: number;
  maxPerTable?: number;
  maxSpace?: number;
  closeRewrite?: boolean;
  rules?: Array<{ ruleCode: string; rewrite: boolean; threshold?: string }>;
  singleQueryFlag?: boolean;
}

export interface CreateAnalysisResponse {
  code: number;
  message: string;
  data: {
    analysisId: string;
    status: string;
  };
}

export interface GetAnalysisSummaryParams {
  userKey: string;
  analysisId: string;
}

export interface GetAnalysisSummaryResponse {
  code: number;
  message: string;
  data: AnalysisSummaryRead;
}

export interface GetStatementDetailsParams {
  userKey: string;
  analysisStmtId: string;
}

export interface GetStatementDetailsResponse {
  code: number;
  message: string;
  data: StatementDetailInfoRead;
}

export interface AnalysisSummaryRead {
  status: string; // 分析状态
  analysisName: string;
  basicSummary: AnalysisSummary;
  analysisRuleInfo: RuleQueries[];
  analysisIndexInfo: string[];
  summaryStatementInfo: SummaryStatementInfo[];
}

export interface AnalysisSummary {
  analysisSummaryId: string;
  analysisId: string;
  numberOfQuery: number;
  numberOfSyntaxError: number;
  numberOfRewrite: number;
  numberOfRewriteRules: number;
  numberOfRewrittenQuery: number;
  numberOfViolations: number;
  numberOfViolatedQuery: number;
  numberOfIndex: number;
  numberOfQueryIndex: number;
  performanceImprove: number;
  summaryMarkdown: string;
  summaryMarkdownZh: string;
  commentCount: number;
  needReply: number;
}

export interface RuleQueries {
  ruleName: string;
  stmtNameStr: string;
}
export interface SummaryStatementInfo {
  analysisStmtId: string;
  stmtId: string;
  stmtName: string;
  stmtType: string;
  stmtText: string;
  costBefore: number;
  costAfter: number;
  numberOfRewrite: number;
  numberOfRewriteRules: number;
  numberOfViolations: number;
  numberOfSyntaxError: number;
  numberOfIndex: number;
  numberOfHitIndex: number;
  performance: number;
  contributingIndices: string;
  commentCount: number;
  needReply: number;
}

export interface StatementDetailInfoRead {
  analysisId: string;
  analysisName: string;
  stmtId: string;
  statementName: string;
  stmtText: string;
  detailMarkdown: string;
  detailMarkdownZh: string;
  openaiOptimizeTextEn: string;
  openaiOptimizeTextZh: string;
  indexRecommended: string[];
  rewrittenQuery: RuleRewrittenQuery[];
  violationRule: RuleRewrittenFragments[];
  validationDetails: ValidationDetails;
}
export interface RuleRewrittenQuery {
  ruleCode: string;
  ruleNameZh: string;
  ruleNameEn: string;
  rewrittenQueriesStr: string;
  violatedQueriesStr: string;
}

export interface RuleRewrittenFragments {
  ruleName: string;
  fragmentsStr: string;
}
export interface ValidationDetails {
  beforeCost: number;
  afterCost: number;
  beforePlan: string;
  afterPlan: string;
  performImprovePer: number;
  stmtText: string;
}

export interface ValidationResult {
  isAvailable: boolean;
  error?: string;
  details?: {
    message: string;
    statusCode?: number;
    errorCode?: string;
  };
}

export interface ValidationOptions {
  timeout?: number;
}

export interface ValidationOptions {
  timeout?: number;
  checkPaths?: string[];
}
